/*
 * PatternReader.java
 *
 * Created on 28 de abril de 2006, 0:08
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pattern;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.FileReader;

import usecase.UseCaseXMLException;

import org.apache.log4j.*;


/**
 *
 * @author Rincew
 */
public class PatternReader 
    implements IPatternReader 
{
    
    File f;
    LineNumberReader reader;
    String name;
    String pattern;
    
    /** Creates a new instance of PatternReader */
    public PatternReader (File f) {
        
        this.f = f;
    }


    /**
     * Resets the stream
     **/
    public java.util.Iterator iterator() {
        try {
            this.reader = new LineNumberReader(new FileReader(f));
            /*if (this.reader.ready () == false)
                log.warn ("ReadBuffer created from file '"
                    +f.toString ()+"' is not ready");*/
                    
            
        } catch (FileNotFoundException ex) {
            String msg = "Error loading pattern file: " + f.toString ();
            msg += "\n" + ex;
            UseCaseXMLException e = new UseCaseXMLException(msg);
            e.setStackTrace (ex.getStackTrace ());
            throw e;
        } catch (IOException ex) {
            log.error ("Error when cheking if buffer is ready.");
            ex.printStackTrace();
            //
        }        
        return this;
        
    }
    
    /** 
     * Esta funcion esta mal, tendra que 
     * hacer un while hasta que encontrara una 
     * linea sin #.
     **/
    public boolean hasNext () {
        boolean b;
        try {
            /*if (this.reader.ready () == false)
                log.warn ("ReadBuffer created from file '"
                    +f.toString ()+"' is not ready");*/

            this.name = this.reader.readLine ();
            //System.out.println ("Name: " + this.name);
            if (this.name != null) {
                if (this.name.length () > 0) {
                    if (this.name.charAt (0) == '#')
                        this.pattern = this.reader.readLine ();
                    else
                       this. pattern = this.name;
                }
                //System.out.println ("Pattern: " + this.pattern);
            }
        
            b = ( (this.name != null) && (this.pattern != null));
            if (b == false) {
                //log.debug ("Closing buffer");
                //this.reader.close ();
                //this.reader.
                
            }
            return b;
        } catch (IOException ex) {
            ex.printStackTrace();
            //
            return false;
        }

    }

    /** **/
    public Object next () {
        return this.pattern;
    }

    /** **/
    public String getPatternName() {
        return this.name;
    }
    
    /** **/
    public void remove () {
        throw new java.lang.UnsupportedOperationException();
    }
    
    /** Test only.
     *  After called this method, a new PatternReader must be created.
     */
    public String toString() {
        String s = "---- PatternReader / File: " + this.f +
            " ---\n";
        java.util.Iterator it = this.iterator ();
            
        while (it.hasNext ()) {
            s += this.getPatternName () + ": " + it.next () + "\n";
            
        }
        return s;
    }
    
    
     //--- Log ------------------------------------------------------------
    static Logger log;
    static {
        log = Logger.getLogger (PatternReader.class.getName());
    }
    
}
